package net.huashitong.supervision.insideDuCha.service;

import com.google.common.util.concurrent.ListenableFuture;
import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysDeptRoom;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysDeptRoomService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.insideDuCha.dao.InsideUserTaskDao;
import net.huashitong.supervision.insideDuCha.entity.InsideCycleEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideUserTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideSubTaskFilter;
import net.huashitong.supervision.insideDuCha.filter.InsideUserTaskFilter;
import net.huashitong.supervision.insideDuCha.util.CommUtil;
import net.huashitong.supervision.task.service.SMSUtilService;
import net.huashitong.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class InsideUserTaskService extends StringPKService<InsideUserTaskEntity> {
    @Autowired
    private InsideUserTaskDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysDeptRoomService roomService;
    @Autowired
    private SMSUtilService smsUtilService;

    @Override
    protected BaseDao<InsideUserTaskEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(InsideUserTaskEntity obj) {
        obj.setSysId(tableService.updateMaxSysId("inside_user_task",null));
    }

    public void save(InsideUserTaskEntity obj,String CreateUserId)
    {
        beforeSave(obj);
        obj.setCreateDate(DateUtil.getCurrentDate());
        obj.setCreateTime(DateUtil.getCurrentTime());
        obj.setCreateUser(CreateUserId);
        dao.save(obj);
    }

    /**
     *  单位人员 任务
     * @param lists
     * @return
     */
    @Transactional
    public List<InsideSubTaskFilter> saveUsetTasks(List<InsideSubTaskFilter> lists ){

        List<InsideUserTaskEntity> listUserTasks = new ArrayList<>();
//        String deptId = SysUserUtils.getDeptId();
        for (InsideSubTaskFilter item : lists) {
            String userIdStr = item.getUserId();
            String[] userIdArry = userIdStr.split(",");//分割多选人员；
            //校验重复userId
            List<String> userList = new ArrayList<>();
            //任务时限；
            String remindDate = CommUtil.remindDate(item.getTimeType(), item.getCycleValue());
            for (String userId : userIdArry) {
                if(!userList.contains(userId)){
                    userList.add(userId);
                }else{
                    continue;
                }
                InsideUserTaskEntity iut = new InsideUserTaskEntity();
                iut.setTaskState("0");
                iut.setDeptId(item.getDeptId());
                iut.setMainTaskId(item.getTaskMainId());
                iut.setSubTaskId(item.getSysId());
                iut.setUserId(userId);
                iut.setRoomId(item.getRoomId());
                iut.setValids(1);
                iut.setBackType("0");
                //DateUtil.nextRemindDate()
                iut.setRemindDate(remindDate);//到期时间
                iut.setTimeRound(1);
                if(StringUtils.isBlank(SysUserUtils.getUserId())){
                    save(iut,item.getCreateUser());
                }else {

                this.save(iut);
                }
                listUserTasks.add(iut);
                item.setUserTaskId(iut.getSysId());
                //listUserTasks.add(iut);
            }

        }


        return lists;


    }


    /**
     * 承办人 任务
     */
    public List<InsideUserTaskFilter> getUserlist (InsideUserTaskFilter filter){

        List<InsideUserTaskEntity> userTasks = dao.getUserTasks(filter);
        List<InsideUserTaskFilter> listfilters = new ArrayList<>();
        for (InsideUserTaskEntity userTask : userTasks) {
            InsideUserTaskFilter itemList = new InsideUserTaskFilter();
            BeanUtils.copyProperties(userTask,itemList);

            SysUser user = userService.getById(userTask.getUserId());
            itemList.setUserName(user.getName());
            if(!"".equals(userTask.getRoomId())&&null!=userTask.getRoomId()){

                SysDeptRoom roomEntity = roomService.getById(userTask.getRoomId());
                itemList.setRoomId(userTask.getRoomId());
                itemList.setRoomName(roomEntity.getRoomName());
            }

            listfilters.add(itemList);
        }

        return listfilters;
    }

    public List<InsideUserTaskEntity> getUserTaskByMainId(String mainTaskId){
        InsideUserTaskFilter filter = new InsideUserTaskFilter();
        filter.setMainTaskId(mainTaskId);
        return dao.getUserTasks(filter);
    }
    public List<InsideUserTaskEntity> getUserTaskBySubId(String subId){
        InsideUserTaskFilter filter = new InsideUserTaskFilter();
        filter.setSubTaskId(subId);
        return dao.getUserTasks(filter);
    }



}
